package com.hyt.it.ogt.kq.service.gov.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.StringUtils;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.constant.BreakRuleExcelParam;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.constant.StringCodeEnum;
import com.hyt.it.ogt.kq.common.gov.utils.StringUtil;
import com.hyt.it.ogt.kq.service.gov.mapper.BreakRuleBehaviorMapper;
import com.hyt.it.ogt.kq.service.gov.mapper.BreakRuleSchemeMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.BreakRuleBehavior;
import com.hyt.it.ogt.kq.service.gov.model.entity.BreakRuleScheme;
import com.hyt.it.ogt.kq.service.gov.model.excel.listener.StringListener;
import com.hyt.it.ogt.kq.service.gov.model.vo.BreakInfoVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.BreakRuleBehaviorVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.SchemeListVO;
import com.hyt.it.ogt.kq.service.gov.service.IBreakRuleBehaviorService;
import com.hyt.it.ogt.kq.service.gov.service.IBreakRuleSchemeService;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeBreakRuleSchemeService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskService;
import com.hyt.model.PageParam;

/**
 * <p>
 * 违纪处理表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@Service
public class BreakRuleSchemeServiceImpl extends BaseServiceImpl<BreakRuleSchemeMapper, BreakRuleScheme> implements IBreakRuleSchemeService {

    @Resource
    private IBreakRuleBehaviorService iBreakRuleBehaviorService;

    @Resource
    private BreakRuleBehaviorMapper breakRuleBehaviorMapper;

    @Resource
    private BreakRuleSchemeMapper breakRuleSchemeMapper;

    @Resource
    private IOfficeBreakRuleSchemeService iOfficeBreakRuleSchemeService;

    @Resource
    private ITaskService iTaskService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateScheme(BreakRuleScheme param) throws KqException {
        iTaskService.isLockDataThrowException(param.getTaskId());
        //参数校验
        this.verifyBreakRuleScheme(param);
        try {
            //更新违纪处理方法表
            breakRuleSchemeMapper.updateScheme(param);
        } catch (DuplicateKeyException e) {
            throw new KqException(ResponseCode.BEHAVIOR_SAME_SCHEME_CODE_EXIST.getCode(),ResponseCode.BEHAVIOR_SAME_SCHEME_CODE_EXIST.getMsg());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addScheme(BreakRuleScheme param) {
        //: 校验任务数据锁定状态(后续版本考虑优化为从缓存中获取状态)
        iTaskService.isLockDataThrowException(param.getTaskId());
        //参数校验
        this.verifyBreakRuleScheme(param);
        //插入违纪处理方法表
        super.save(param);
    }

    protected void verifyBreakRuleScheme(BreakRuleScheme param) {
        //参数校验
        if (!this.checkBanYears(param.getBanYears())) {
            //停考年限只能是非负整数字
            throw new KqException(ResponseCode.BEHAVIOR_SCHEME_STOP_YEAR_NUMBER_INVALID.getCode(),ResponseCode.BEHAVIOR_SCHEME_STOP_YEAR_NUMBER_INVALID.getMsg());
        }
        if (!this.checkCode(param.getCode())) {
            //违纪处理方法代码只能是1~10位
            throw new KqException(ResponseCode.BEHAVIOR_SCHEME_CODE_FORMAT_INVALID.getCode(), ResponseCode.BEHAVIOR_SCHEME_CODE_FORMAT_INVALID.getMsg());
        }
        LambdaQueryWrapper<BreakRuleScheme> query = Wrappers.<BreakRuleScheme>lambdaQuery()
                .eq(BreakRuleScheme::getTaskId, param.getTaskId())
                .eq(BreakRuleScheme::getCode, param.getCode());
        if (org.apache.commons.lang3.StringUtils.isNotBlank(param.getId())) {
            query.ne(BreakRuleScheme::getId, param.getId());
        }
        //检查违纪处理代码是否重复
        int count = super.count(query);
        if (count > 0) {
            //当前考试任务下存在相同的违纪处理代码
            throw new KqException(ResponseCode.BEHAVIOR_SAME_SCHEME_CODE_EXIST.getCode(),ResponseCode.BEHAVIOR_SAME_SCHEME_CODE_EXIST.getMsg());
        }
    }

    @Override
    public Page<BreakRuleBehaviorVO> selectPageBreakRuleBehavior(PageParam<BreakRuleBehaviorVO> pageParam,
                                                                 String taskId, String key) {
        return breakRuleBehaviorMapper.selectPageBreakRuleBehavior(pageParam, taskId, key);
    }

    @Override
    public Page<BreakRuleScheme> selectPageBreakRuleScheme(PageParam<BreakRuleScheme> pageParam, String taskId,
                                                           String key) {
        LambdaQueryWrapper<BreakRuleScheme> schemeQueryWrapper = Wrappers.<BreakRuleScheme>lambdaQuery()
                .eq(BreakRuleScheme::getTaskId, taskId)
                .eq(BreakRuleScheme::getDelFlag, false)
                .orderByAsc(BreakRuleScheme::getCode);
        if(org.apache.commons.lang3.StringUtils.isNotBlank(key)) {
            schemeQueryWrapper.and(wrapper -> {
                wrapper.like(BreakRuleScheme::getCode, key).or().like(BreakRuleScheme::getContent, key);
            });
        }
        return baseMapper.selectPage(pageParam, schemeQueryWrapper);
    }

    @Override
    public List<SchemeListVO> queryScheme(String taskId) {
        return breakRuleSchemeMapper.queryScheme(taskId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteScheme(String ids, String taskId) {
        //: 校验任务数据锁定状态(后续版本考虑优化为从缓存中获取状态)
        iTaskService.isLockDataThrowException(taskId);
        //ids字符串转list
        List<String> schemeIds = StringUtil.stringToList(ids);
        //删除违纪处理方法
        super.removeByIds(schemeIds);
        //更新违纪行为表中关联的违纪处理方法代码为null
        breakRuleBehaviorMapper.updateSchemeCodeNull(schemeIds, taskId);
    }

    @Override
    public BreakInfoVO queryAll(String taskId) {
        //查询违纪处理方法
        QueryWrapper<BreakRuleScheme> schemeQueryWrapper = new QueryWrapper<>();
        schemeQueryWrapper.lambda()
                .eq(BreakRuleScheme::getTaskId, taskId)
                .eq(BreakRuleScheme::getDelFlag, false)
                .orderByAsc(BreakRuleScheme::getCode);
        List<BreakRuleScheme> breakRuleSchemeList = list(schemeQueryWrapper);
        //查询违纪行为
        List<BreakRuleBehaviorVO> breakRuleBehaviorList = breakRuleBehaviorMapper.selectBehavior(taskId);
        //组装数据
        BreakInfoVO breakInfoVO = new BreakInfoVO();
        breakInfoVO.setBreakRuleScheme(breakRuleSchemeList)
                .setBreakRuleBehavior(breakRuleBehaviorList);
        return breakInfoVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file, String taskId) throws KqException, IOException {
        //: 校验任务数据锁定状态(后续版本考虑优化为从缓存中获取状态)
        iTaskService.isLockDataThrowException(taskId);
        //校验excel文件
        this.excelVerify(file);
        //读取excel
        List<List<List<String>>> excelData = this.readExcel(file);
        //校验excel结构
        this.verityStructure(excelData);
        //解析并校验违纪处理方法数据
        List<BreakRuleScheme> schemeList = this.analysisSchemeData(excelData, taskId);
        //解析并校验违纪行为数据
        List<BreakRuleBehavior> behaviorList = this.analysisBehaviorData(excelData, schemeList, taskId);
        //插入数据库
        this.insertBreakRuleInfo(schemeList, behaviorList, taskId);
    }

    /**
     * 初始化数据，默认从机构级别的数据拉取
     * @param taskId
     * @param officeId
     */
    @Override
    public void initBreakRuleSchemeFromOffice(String taskId, String officeId) {

        Integer count = this.lambdaQuery()
                .eq(BreakRuleScheme::getTaskId,taskId)
                .eq(BreakRuleScheme::getDelFlag,false)
                .count();
        if (count <= 0) {
            //1.机构数据初始化
            iOfficeBreakRuleSchemeService.initOfficeBreakRuleSchemeFromOffice(officeId);
            //2.任务数据初始化
            breakRuleSchemeMapper.initBreakRuleSchemeFromOffice(taskId,officeId);

        }
    }

    /**
     * 插入数据库
     *
     * @param schemeList   违纪处理方法数据
     * @param behaviorList 验违纪行为数据
     * @param taskId       考试任务id
     */
    protected void insertBreakRuleInfo(List<BreakRuleScheme> schemeList, List<BreakRuleBehavior> behaviorList, String taskId) {
        //查询当前违纪信息
        BreakInfoVO breakInfoVO = this.queryAll(taskId);
        List<BreakRuleScheme> curScheme = breakInfoVO.getBreakRuleScheme();
        if (!CollectionUtils.isEmpty(curScheme)) {
            //比较导入的违纪处理方法代码是否重复，并设置排序号
            for (BreakRuleScheme scheme : schemeList) {
                String code = scheme.getCode();
                for (BreakRuleScheme cur : curScheme) {
                    String curCode = cur.getCode();
                    if (curCode.equals(code)) {
                        //导入的Excel中存在已有的违纪处理代码
                        throw new KqException(ResponseCode.IMPORT_SAME_SCHEME_CODE_EXIST.getCode(),ResponseCode.IMPORT_SAME_SCHEME_CODE_EXIST.getMsg());
                    }
                }
            }
        }
        List<BreakRuleBehaviorVO> curBehavior = breakInfoVO.getBreakRuleBehavior();
        if (!CollectionUtils.isEmpty(curBehavior)) {
            //比较导入的违纪行为代码是否重复，并设置排序号
            for (BreakRuleBehavior behavior : behaviorList) {
                String code = behavior.getCode();
                for (BreakRuleBehavior cur : curBehavior) {
                    String curCode = cur.getCode();
                    if (curCode.equals(code)) {
                        //导入的Excel中存在已有的违纪行为代码
                        throw new KqException(ResponseCode.IMPORT_SAME_BEHAVIOR_CODE_EXIST.getCode(), ResponseCode.IMPORT_SAME_BEHAVIOR_CODE_EXIST.getMsg());
                    }
                }
            }
        }
        try {
            //数据插入数据库
            super.saveBatch(schemeList);
        } catch (DuplicateKeyException e) {
            throw new KqException(ResponseCode.IMPORT_SAME_SCHEME_CODE_EXIST.getCode(),ResponseCode.IMPORT_SAME_SCHEME_CODE_EXIST.getMsg());
        }
        try {
            iBreakRuleBehaviorService.saveBatch(behaviorList);
        } catch (DuplicateKeyException e) {
            throw new KqException(ResponseCode.IMPORT_SAME_BEHAVIOR_CODE_EXIST.getCode(), ResponseCode.IMPORT_SAME_BEHAVIOR_CODE_EXIST.getMsg());
        }
    }

    /**
     * 解析并校验违纪行为数据
     *
     * @param excelData  excel数据
     * @param schemeList 违纪处理方法数据
     * @param taskId     考试任务id
     * @return 解析后的违纪行为数据
     */
    protected List<BreakRuleBehavior> analysisBehaviorData(List<List<List<String>>> excelData,
                                                           List<BreakRuleScheme> schemeList, String taskId) {
        List<BreakRuleBehavior> behaviorList = new ArrayList<>();
        List<List<String>> behaviorRowList = excelData.get(BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_INDEX);
        Map<String, String> codeMap = new HashMap<>(16);
        schemeList.forEach(scheme -> codeMap.put(scheme.getCode(), scheme.getId()));
        for (int i = 1; i < behaviorRowList.size(); i++) {
            BreakRuleBehavior breakRuleBehavior = new BreakRuleBehavior();
            List<String> rowData = behaviorRowList.get(i);
            String code = rowData.get(BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_CODE_INDEX);
            if (!this.checkCode(code)) {
                //违纪行为sheet中存在不合法的违纪行为代码，违纪行为代码最多10字母加数字
                throw new KqException(ResponseCode.BEHAVIOR_CODE_FORMAT_INVALID.getCode(),ResponseCode.BEHAVIOR_CODE_FORMAT_INVALID.getMsg());
            }
            String content = rowData.get(BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_CONTENT_INDEX);
            if (StringUtils.isEmpty(content)) {
                //违纪行为sheet中存在空的违纪行为描述
                throw new KqException(ResponseCode.BEHAVIOR_EXIST_DESC_EMPTY.getCode(),ResponseCode.BEHAVIOR_EXIST_DESC_EMPTY.getMsg());
            }
            String schemeCode = rowData.get(BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_SCHEME_CODE_INDEX);
            if (StringUtils.isEmpty(schemeCode)) {
                //违纪行为sheet中存在空的违纪处理代码
                throw new KqException(ResponseCode.SCHEME_CODE_EMPTY_SHEET.getCode(),ResponseCode.SCHEME_CODE_EMPTY_SHEET.getMsg());
            }
            String schemeId = codeMap.get(schemeCode);
            if (schemeId == null) {
                //违纪行为sheet中有违纪处理方法sheet中不存在的违纪处理代码
                throw new KqException(ResponseCode.BEHAVIOR_SCHEME_ABOUT_MATCH_NULL_CODE.getCode(),ResponseCode.BEHAVIOR_SCHEME_ABOUT_MATCH_NULL_CODE.getMsg());
            }
            breakRuleBehavior.setTaskId(taskId)
                    .setCode(code)
                    .setContent(content)
                    .setSchemeId(schemeId)
                    .setSort(i);
            behaviorList.add(breakRuleBehavior);
        }
        List<String> behaviorCodeList = new ArrayList<>();
        behaviorList.forEach(behavior -> behaviorCodeList.add(behavior.getCode()));
        long distinctCount = behaviorCodeList.stream().distinct().count();
        if (behaviorCodeList.size() != distinctCount) {
            //违纪行为sheet中存在重复的违纪处理代码
            throw new KqException(ResponseCode.REPEAT_BEHAVIOR_CODE_REPEAT_ERROR.getCode(), ResponseCode.REPEAT_BEHAVIOR_CODE_REPEAT_ERROR.getMsg());
        }
        return behaviorList;
    }

    /**
     * 解析并校验违纪处理方法数据
     *
     * @param excelData excel数据
     * @param taskId    考试任务id
     * @return 解析结果
     */
    protected List<BreakRuleScheme> analysisSchemeData(List<List<List<String>>> excelData, String taskId) {
        List<BreakRuleScheme> schemeList = new ArrayList<>();
        List<List<String>> schemeObjectList = excelData.get(BreakRuleExcelParam.BREAK_RULE_SCHEME_INDEX);
        for (int i = 1; i < schemeObjectList.size(); i++) {
            BreakRuleScheme breakRuleScheme = new BreakRuleScheme();
            List<String> rowData = schemeObjectList.get(i);
            String code = rowData.get(BreakRuleExcelParam.BREAK_RULE_SCHEME_CODE_INDEX);
            if (!this.checkCode(code)) {
                //违纪处理方法sheet中存在不合法的违纪处理代码，违纪处理代码最多是10位字符
                throw new KqException(ResponseCode.BEHAVIOR_SCHEME_CODE_FORMAT_INVALID.getCode(), ResponseCode.BEHAVIOR_SCHEME_CODE_FORMAT_INVALID.getMsg());
            }
            String content = rowData.get(BreakRuleExcelParam.BREAK_RULE_SCHEME_CONTENT_INDEX);
            if (StringUtils.isEmpty(content)) {
                //违纪处理方法sheet中存在空的违纪处理办法
                throw new KqException(ResponseCode.SCHEME_CODE_EMPTY_SHEET.getCode(), ResponseCode.SCHEME_CODE_EMPTY_SHEET.getMsg());
            }
            String banYears = rowData.get(BreakRuleExcelParam.BREAK_RULE_SCHEME_BAN_YEARS_INDEX);
            if (!this.checkBanYears(banYears)) {
                //违纪处理方法sheet中存在不合法的停考年限，停考年限只能是非负整数字
                throw new KqException(ResponseCode.IMPORT_BEHAVIOR_STOP_AGE_PARAM_INVALID.getCode(),ResponseCode.IMPORT_BEHAVIOR_STOP_AGE_PARAM_INVALID.getMsg());
            }
            breakRuleScheme.setTaskId(taskId)
                    .setCode(code)
                    .setContent(content)
                    .setBanYears(Integer.valueOf(banYears))
                    .setSort(i).setId(UUIDUtils.newSortUUID());
            schemeList.add(breakRuleScheme);
        }
        List<String> codeList = new ArrayList<>();
        schemeList.forEach(scheme -> codeList.add(scheme.getCode()));
        long distinctCount = codeList.stream().distinct().count();
        if (codeList.size() != distinctCount) {
            //违纪处理方法sheet中存在重复的违纪处理代码
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_REPEAT_SCHEME.getCode(),ResponseCode.IMPORT_BEHAVIOR_REPEAT_SCHEME.getMsg());
        }
        return schemeList;
    }

    private boolean checkBanYears(Integer banYears) {
        return banYears >= 0;
    }

    private boolean checkBanYears(String banYears) {
        if (StringUtils.isEmpty(banYears)) {
            return false;
        }
        String patternStr = "^[1-9]\\d*$";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher match = pattern.matcher(banYears);
        return match.matches();
    }

    @Override
    public boolean checkCode(String code) {
        if (StringUtils.isEmpty(code)) {
            return false;
        }
        String patternStr = "[0-9A-Za-z]{1,10}";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher match = pattern.matcher(code);
        return match.matches();
    }

    @Override
    public List<BreakRuleScheme> listByTaskId(String taskId) {
        QueryWrapper<BreakRuleScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BreakRuleScheme::getTaskId, taskId)
                .eq(BreakRuleScheme::getDelFlag, false);
        return list(queryWrapper);
    }

    /**
     * 校验excel结构
     *
     * @param excelData excel数据
     * @throws KqException 校验不通过，抛出异常
     */
    protected void verityStructure(List<List<List<String>>> excelData) throws KqException {
        //校验sheet数量
        if (excelData.size() != BreakRuleExcelParam.SHEET_COUNT) {
            //sheet数量不正确
            throw new KqException(ResponseCode.BREAK_RULE_EXCEL_NUMBER_FAIL.getCode(), ResponseCode.BREAK_RULE_EXCEL_NUMBER_FAIL.getMsg());
        }
        //校验违纪处理方法sheet结构
        List<List<String>> schemeList = excelData.get(BreakRuleExcelParam.BREAK_RULE_SCHEME_INDEX);
        if (CollectionUtils.isEmpty(schemeList)) {
            //纪处理方法sheet为空
            throw new KqException(ResponseCode.BREAK_RULE_IMPORT_SCHEME_EMPTY.getCode(),ResponseCode.BREAK_RULE_IMPORT_SCHEME_EMPTY.getMsg());
        }
        //校验违纪处理方法表头
        List<String> schemeHeader = schemeList.get(BreakRuleExcelParam.BREAK_RULE_SCHEME_HEADER_INDEX);
        if (schemeHeader.size() != BreakRuleExcelParam.BREAK_RULE_SCHEME_COLUMN_COUNT) {
            //违纪处理方法sheet表头结构错误
            throw new KqException(ResponseCode.IMPORT_SCHEME_HEAD_WRONG.getCode(),ResponseCode.IMPORT_SCHEME_HEAD_WRONG.getMsg());
        }
        //校验数据行数
        int schemeRowSize = schemeList.size();
        if (schemeRowSize - 1 > BreakRuleExcelParam.MAX_ROW_SIZE) {
            //违纪处理方法数据超过最大行数
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_MAX_SIZE.getCode(),ResponseCode.IMPORT_BEHAVIOR_MAX_SIZE.getMsg());
        }
        if (schemeRowSize < BreakRuleExcelParam.MIN_ROW_SIZE) {
            //违纪处理方法数据小于最小行数
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_MIN_SIZE_INVALID.getCode(),ResponseCode.IMPORT_BEHAVIOR_MIN_SIZE_INVALID.getMsg());
        }
        //校验数据列数
        for (int i = 1; i <= schemeRowSize - 1; i++) {
            List<String> rowData = schemeList.get(i);
            if (rowData.size() > BreakRuleExcelParam.BREAK_RULE_SCHEME_COLUMN_COUNT) {
                //违纪处理方法sheet列数错误
                throw new KqException(ResponseCode.IMPORT_BEHAVIOR_COLUMN_WRONG.getCode(),ResponseCode.IMPORT_BEHAVIOR_COLUMN_WRONG.getMsg());
            }
        }

        //校验违纪行为sheet结构
        List<List<String>> behaviorList = excelData.get(BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_INDEX);
        if (CollectionUtils.isEmpty(behaviorList)) {
            //校验违纪行为sheet为空
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_IS_EMPTY.getCode(),ResponseCode.IMPORT_BEHAVIOR_IS_EMPTY.getMsg());
        }
        //校验违纪行为表头
        List<String> behaviorHeader = behaviorList.get(BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_HEADER_INDEX);
        if (behaviorHeader.size() != BreakRuleExcelParam.BREAK_RULE_SCHEME_COLUMN_COUNT) {
            //验违纪行为sheet表头结构错误
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_HEAD_WRONG.getCode(), ResponseCode.IMPORT_BEHAVIOR_HEAD_WRONG.getMsg());
        }
        //校验数据行数
        int behaviorRowSize = behaviorList.size();
        if (behaviorRowSize - 1 > BreakRuleExcelParam.MAX_ROW_SIZE) {
            //违纪行为数据超过最大行数
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_MAX_ROW_SIZE.getCode(),ResponseCode.IMPORT_BEHAVIOR_MAX_ROW_SIZE.getMsg());
        }
        if (behaviorRowSize < BreakRuleExcelParam.MIN_ROW_SIZE) {
            //违纪行为数据小于最小行数
            throw new KqException(ResponseCode.IMPORT_BEHAVIOR_MIN_ROW_SIZE_INVALID.getCode(),ResponseCode.IMPORT_BEHAVIOR_MIN_ROW_SIZE_INVALID.getMsg());
        }
        //校验数据列数
        for (int i = 1; i <= behaviorRowSize - 1; i++) {
            List<String> rowData = behaviorList.get(i);
            if (rowData.size() > BreakRuleExcelParam.BREAK_RULE_BEHAVIOR_COLUMN_COUNT) {
                //违纪行为sheet列数错误
                throw new KqException(ResponseCode.IMPORT_BEHAVIOR_COLUMN_WRONG_SHEET.getCode(), ResponseCode.IMPORT_BEHAVIOR_COLUMN_WRONG_SHEET.getMsg());
            }
        }
    }

    /**
     * 读取excel数据
     *
     * @param file excel文件
     * @return 读取完毕后的excel内容
     * @throws IOException IO异常
     */
    protected List<List<List<String>>> readExcel(MultipartFile file) throws IOException {
        //读取excel
        List<List<List<String>>> excelData = new ArrayList<>();
        StringListener breakRuleSchemeListener = new StringListener();
        StringListener breakRuleBehaviorListener = new StringListener();
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(file.getInputStream()).build();
            //读取违纪处理方法sheet
            ReadSheet schemeSheet = EasyExcel.readSheet(0)
                    .registerReadListener(breakRuleSchemeListener).build();
            //读取违纪行为
            ReadSheet behaviorSheet = EasyExcel.readSheet(1)
                    .registerReadListener(breakRuleBehaviorListener).build();
            // 这里注意 一定要把sheet1 sheet2 一起传进去，不然有个问题就是03版的excel 会读取多次，浪费性能
            excelReader.read(schemeSheet, behaviorSheet);
            excelData.add(breakRuleSchemeListener.getList());
            excelData.add(breakRuleBehaviorListener.getList());
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        return excelData;
    }

    protected void excelVerify(MultipartFile file) throws KqException {
        if (file.isEmpty()) {
            //导入文件不存在
            throw new KqException(ResponseCode.BEHAVIOR_IMPORT_FILE_NON_EXIST.getCode(), ResponseCode.BEHAVIOR_IMPORT_FILE_NON_EXIST.getMsg());
        }
        String fileName = file.getOriginalFilename();

        if (fileName == null) {
            //导入违纪信息文件类型错误
            throw new KqException(ResponseCode.IMPORT_BREAK_RULE_FILE_WRONG.getCode(), ResponseCode.IMPORT_BREAK_RULE_FILE_WRONG.getMsg());
        }
        String ext = FilenameUtils.getExtension(fileName);
        if (!ext.equalsIgnoreCase(StringCodeEnum.EXCEL_XLS.getCode())
                && !ext.equalsIgnoreCase(StringCodeEnum.EXCEL_XLSX.getCode())) {
            //导入违纪信息文件类型错误
            throw new KqException(ResponseCode.IMPORT_BREAK_RULE_FILE_WRONG.getCode(), ResponseCode.IMPORT_BREAK_RULE_FILE_WRONG.getMsg());
        }
    }
}
